<template>
	<div >
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						<el-col :span="8" class="colBlock">
							<el-form-item label="创建时间">
								<el-date-picker v-model="tableData.dateRange" type="datetimerange" size="default"
									style="width: 300px" range-separator="-" start-placeholder="开始日期"
									end-placeholder="结束日期"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="8" class="colBlock">
							<el-form-item label="账号" prop="account">
								<el-input v-model="tableData.param.account" placeholder="请输入账号" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="经理名称" prop="nickName">
								<el-input v-model="tableData.param.nickName" placeholder="请输入经理名称" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="状态" prop="status">
								<el-select v-model="tableData.param.status" placeholder="请选择状态" clearable>
									<el-option label="开启" value="ENABLED" />
									<el-option label="关闭" value="DISABLED" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="!showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll">
										<ArrowUp />
									</el-icon>
									<el-icon v-show="!showAll">
										<ArrowDown />
									</el-icon>
								</el-button>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll"><ArrowUp /></el-icon>
									<el-icon v-show="!showAll"><ArrowDown /></el-icon>
								</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<el-row :gutter="10" class="mb8"  v-if="permissionStore.isPermissionsBtn('004001001')">
					<el-col :span="1.5">
						<el-button type="primary" @click="handleAdd"><el-icon>
								<Plus />
							</el-icon>新增</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="账号" align="center" prop="account" min-width="100px" />
				<el-table-column label="经理名称" align="center" prop="nickName" min-width="100px" />
				<el-table-column label="邀请码" align="center" prop="id" min-width="100px" />
				<el-table-column label="已认证企业" align="center" prop="verifiedNum" min-width="100px" />
				<el-table-column label="未认证企业" align="center" prop="unVerifiedNum" min-width="100px" />
				<el-table-column label="企业累计充值金额" align="center" prop="totalRecharge" min-width="100px" />
				<el-table-column label="状态" align="center" min-width="100px" >
					<template #default="scope">
						<el-switch v-model="scope.row.statusType" :disabled="!permissionStore.isPermissionsBtn('004001003')" active-value="启用" inactive-value="停用"
							@change="handleStatusChange(scope.row)" />
					</template>
				</el-table-column>
				<el-table-column label="创建时间" align="center" prop="createTimeTxt" min-width="100px" />
				<el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
						<el-button type="primary" link @click="handleView(scope.row)"><el-icon>
								<View />
							</el-icon>详情</el-button>
						<el-button  v-if="permissionStore.isPermissionsBtn('004001002')" text type="primary"
							@click="handleResetPwd(scope.row)"><el-icon>
								<EditPen />
							</el-icon>重置密码</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<addView ref="addRef" @getList="getList"></addView>
		<detailView ref="detailRef" @getList="getList"></detailView>
	</div>
</template>
<script lang="ts">
import { managerListApi, editManagerStatusApi } from '@/api/customerManagerManage/customerManager';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { formatDateTime, dateToTimestamp } from '@/utils/index'
import { usePermissionStore } from "@/store/modules/permission"
import dictionary from '@/utils/dictionary.json';
import addView from './component/add.vue';
import detailView from './component/detail.vue';
import { resetUserPasswordApi } from '@/api/auth/user';
export default defineComponent({
	name: 'customerManagerList',//客户经理管理
	components: {
		addView,
		detailView,
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref();
		const editRef = ref();
		const detailRef = ref();
		const permissionStore = usePermissionStore()
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			ids: [],
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange: [],
				param: {
					pageNumber: 1,
					pageLength: 10,
					// phone: '',
					status: null,
					beginTime: null,
					endTime: null,
					nickName:null,
					account:null,
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			state.tableData.dateRange = []
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			if (state.tableData.dateRange.length > 0) {
				state.tableData.param.beginTime = dateToTimestamp(state.tableData.dateRange[0])
				state.tableData.param.endTime = dateToTimestamp(state.tableData.dateRange[1])
			} else {
				state.tableData.param.beginTime = null
				state.tableData.param.endTime = null
			}
			if (!state.tableData.param.status) state.tableData.param.status = null
			managerListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list.map(item => {
					item.createTimeTxt = formatDateTime(item.createTime)
					item.statusType = dictionary[item.status]
					return item
				})
				console.log(state.tableData.data);
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		const toggleSearch = () => {
			showAll.value = !showAll.value;
		};

		const handleAdd = () => {
			addRef.value.openDialog();
		};

		const handleView = (row) => {
			detailRef.value.openDialog(toRaw(row));
		};

		/** 重置密码按钮操作 */
		const handleResetPwd = (row: any) => {
			ElMessageBox.prompt('请输入"' + row.account + '"的新密码', "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消"
			}).then(({ value }) => {
				if (!value || value == '') {
					ElMessage.error('密码不能为空');
					return
				}
				resetUserPasswordApi({ userName: row.account, password: value }).then(() => {
				  ElMessage.success("修改成功，新密码是：" + value);
				});
			}).catch(() => { });
		};

		// 状态修改
		const handleStatusChange = (row: any) => {
			row.status = row.status == 'ENABLED' ? 'DISABLED' : 'ENABLED';
			let text = row.statusType
			ElMessageBox.confirm('确认要"' + text + '"："' + row.nickName + '"吗?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return editManagerStatusApi({ id: row.id, status: row.status });
			}).then(() => {
				ElMessage.success(text + "成功");
			}).catch(function () {
				row.status = row.status == 'ENABLED' ? 'DISABLED' : 'ENABLED';
				row.statusType = dictionary[row.status]
			});
		};

		return {
			proxy,
			addRef,
			editRef,
			detailRef,
			showAll,
			loading,
			word,
			queryRef,
			dictionary,
			permissionStore,
			resetQuery,
			getList,
			toggleSearch,
			handleAdd,
			handleView,
			handleResetPwd,
			handleStatusChange,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
